package com.ks.phone.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.ks.basictools.ActivityManager;
import com.ks.basictools.BaseActivity;
import com.ks.phone.R;
import com.ks.phone.UserManager;
import com.ks.phone.manager.LogcatFileManager;
import com.ks.phone.manager.PreferenceManager;
import com.ks.phone.responseBean.UserResponse;
import com.ks.phone.wedget.PopupTip;
import com.permissionx.guolindev.PermissionX;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity {

    private PopupTip popupTip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        long l = System.currentTimeMillis();
        if (l > 1717131401000L) {// 截止时间：2024-05-31 12:56:41
            if (popupTip == null) {
                popupTip = new PopupTip(this);
                popupTip.setOnConfirmClickListener(v ->
                        ActivityManager.getInstance().AppExit(SplashActivity.this));
            }
            popupTip.setContent("测试版本已到期");
            popupTip.showPopupWindow();
            return;
        }

        if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            if (popupTip == null) {
                popupTip = new PopupTip(this);
                popupTip.setOnConfirmClickListener(v ->
                        ActivityManager.getInstance().AppExit(SplashActivity.this));
            }
            popupTip.setContent("暂不支持大于Android10的手机");
            popupTip.showPopupWindow();
            return;
        }

        PermissionX.init(this)
                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
                        , Manifest.permission.READ_EXTERNAL_STORAGE)
                .explainReasonBeforeRequest()
                .onExplainRequestReason((scope, deniedList) -> scope.showRequestReasonDialog(deniedList, "即将申请的权限是程序必须依赖的权限", "知道了"))
                .onForwardToSettings((scope, deniedList) -> scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中去手动开启权限", "知道了"))
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        // 开启日志本地文件记录
                        LogcatFileManager.startLogcat();
                        // 开始逻辑
                        start();
                    }
                });
    }

    private void start() {
//        if (TextUtils.isEmpty(recordPath)) {
//            if (popupTip == null) {
//                popupTip = new PopupTip(this);
//                popupTip.setOnConfirmClickListener(v ->
//                        ActivityManager.getInstance().AppExit(SplashActivity.this));
//            }
//            popupTip.showPopupWindow();
//        } else {
        new Handler().postDelayed(() -> {
            boolean login = !TextUtils.isEmpty(PreferenceManager.getInstance().getToken(SplashActivity.this));
            if (!login) {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            } else {
                String json = PreferenceManager.getInstance().getUser(SplashActivity.this);
                UserResponse.UserDTO user = new Gson().fromJson(json, UserResponse.UserDTO.class);
                UserManager.getInstance().saveUser(user);
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
            ActivityManager.getInstance().finishActivity(SplashActivity.class);
        }, 2000);
//        }
    }
}